<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2017/8/24  19:25
 */
namespace app\admin\controller;
use think\Request;
use think\Db;
use think\Page;
class Order extends Common
{
    /**
     * 自动收货
     */
    public  function auto(){
   /*     $list = db('order')->where('status=2 and refundid=0')->select();
       foreach($list as $v){
           if(time()-$v['create_time']-7*60*60*24>0){
               $id=$v['id'];
               $data['finish_time']=$v['create_time']+7*60*60*24;
               $data['status']=3;
               $list = db('order') ->where("id='$id'")->update($data);
               if($v['pay_type']==1){
                   if( $list['pay_type']==1) {
                       db("member")->where("id=" . $v['user_id'])->setInc("credit",$list['price']);
                   }
               }

           }
       }*/
    }

    public function order_list()
    {
        $where=array();
        $status=input('status');
        if(!empty($status)){
            $where['o.status'] =$status;
        }
        if(input("type")!=''){
            $where['o.type'] =input("type");
        }
        $keywords=trim(input('keywords'));
        if(!empty($keywords)){//全部订单
            $where['o.order_sn'] =array("like",'%'.$keywords.'%');
        }
        $member=trim(input('member'));
        if(!empty($member)){//全部订单
            $where['m.nickname|m.mobile'] =array("like",'%'.$member.'%');
        }
        $starttime=trim(input('starttime'));
        if(!empty($starttime)){//全部订单
            $where['o.create_time'] =array("gt",strtotime($starttime));
        }
        $endtime=trim(input('endtime'));
        if(!empty($starttime)){//全部订单
            $where['o.create_time'] =array("gt",strtotime($endtime));
        }
        if(!empty($starttime) && !empty($endtime)){//全部订单
            $where['o.create_time'] =array("between",[strtotime($starttime),strtotime($endtime)]);
        }
        if(input('export')){
            $this->export($where);exit;
        }
        $list = db("order")->alias("o")
            ->field("o.*,m.nickname,m.mobile,m.headimage")
            ->join("member m","o.user_id=m.id")
            ->where($where)
            ->order("o.id desc")
            ->paginate(10);
        $count = db("order")->alias("o")
            ->field("o.*,m.nickname,m.mobile,m.headimage")
            ->join("member m","o.user_id=m.id")
            ->where($where)
            ->count();
        $pay_type=array(
            "0"=>"待支付",
            "1"=>"支付宝",
            "2"=>"支付宝",
            "3"=>"微信",
            "4"=>"百度钱包",
            "5"=>"京东",
            "6"=>"qq钱包",
            "7"=>"NFC支付",
            "8"=>"拉卡拉钱包",
            "15"=>"拉卡拉微信",
            "16"=>"招商银行",
            "17"=>"银联二维码"
        );
        $this->assign("page",$list->render());
        $this->assign("list",$list);
        $this->assign("count",$count);
        $this->assign("pay_type",$pay_type);
        $this->assign("starttime",$starttime);
        $this->assign("endtime",$endtime);
        $this->assign("type",input("type"));
        $this->assign("status",input("status"));

        return $this->fetch();
    }
    /**
     * 订单列表导出excel
     */
    public function export($where)
    {

        $headArr = array("订单号","昵称","收货人姓名","收货号码","省份","城市","区/县","详细地址","下单时间","支付时间",
            '发货时间','完成时间',"商品名称","数量","规格","价格","状态");
        //$where['status']=1;
        $list = db("order")->alias("o")
            ->field("o.*,m.nickname,m.headimage,g.goods_name,g.goods_num,g.goods_price,g.spec_name")
            ->join("member m","o.user_id=m.id")
            ->join("order_goods g","g.order_id=o.id")
            ->order("id desc")
            ->where($where)
            ->select();
        $data=array();
        foreach($list as $k => $v){
            $data[$k][] = $v['order_sn'];
            $data[$k][] = $v['nickname'];
            $data[$k][] = $v['name'];
            $data[$k][] = $v['mobile'];
            $data[$k][] = $v['province'];
            $data[$k][] = $v['city'];
            $data[$k][] = $v['county'];
            $data[$k][] = $v['address'];
            $data[$k][] = date("Y-m-d H:i",$v['create_time']);
            $data[$k][] =!empty($v['pay_time'])?date("Y-m-d H:i",$v['pay_time']):'';
            $data[$k][] = !empty($v['shipping_time'])?date("Y-m-d H:i",$v['shipping_time']):'';
            $data[$k][] = !empty($v['finish_time'])?date("Y-m-d H:i",$v['finish_time']):'';
            $data[$k][] = $v['goods_name'];
            $data[$k][] = $v['goods_num'];
            $data[$k][] = $v['spec_name'];
            $data[$k][] = $v['goods_price'];
            //,0待付款，1已付款，2已发货，3已完成,,4退款中，-2已关闭，-1已退款
            switch($v['status']){
                case '0': $data[$k][]='待付款';break;
                case '1': $data[$k][]='已付款';break;
                case '2': $data[$k][]='已发货';break;
                case '3': $data[$k][]='已完成';break;
                case '4': $data[$k][]='退款中';break;
                case '-1': $data[$k][]='已退款';break;
                case '-2': $data[$k][]='已关闭';break;
            }
        }
        $filename='订单列表';
        (new Export())->ImportExcel($filename,$headArr,$data);
    }
    //订单详情
    public function detail()
    {
        $id = input('order_id');
        $condition['o.id'] = $id;
        if (!$id) {
            $this->error('参数有误');
        }
        $order = db('order')->alias('o')->join('member m', 'o.user_id=m.id')
            ->field('m.nickname,m.mobile,o.*')
            ->where($condition)
            ->find();
        $where['o.id'] = $id;
        $order_goods = db('order')->alias('o')
            ->field('o.*,g.goods_id,g.goods_name,g.goods_num,g.goods_price,g.market_price,spec_name,g.goods_image,g.param')
            ->join('order_goods g','g.order_id=o.id')
            ->where($where)
            ->select();
        foreach($order_goods as &$v){
            $v['goods_image']=IMG_PATH.$v['goods_image'];
        }
        unset($v);

        $system_balance_money = db('system_balance')->where(['order_id'=>$id])->value('money');
        if(empty($system_balance_money)){
            $system_balance_money = 0;
        }
        $member_balance_money = db('member_balance')->where(['order_id'=>$id])->value('money');
        if(empty($member_balance_money)){
            $member_balance_money = 0;
        }
        //商家所得金额
        $shop_stream = db('order_stream')->where(['order_id'=>$id])->value('money');
        if(empty($shop_stream)){
            $shop_stream = 0;
        }
        //商家自提佣金
        $mention_balance = db('mention_balance')->where(['order_id'=>$id])->field('shop_id,money')->find();
        $realname = db('shop')->where(['id'=>$mention_balance['shop_id']])->value('realname');

        $this->assign('realname',$realname);
        $this->assign('mention_balance',$mention_balance);
        $this->assign('order_goods', $order_goods);
        $this->assign('system_balance_money', $system_balance_money);
        $this->assign('member_balance_money', $member_balance_money);
        $this->assign('shop_stream', $shop_stream);
        $this->assign('order', $order);
        $this->assign('id', $id);

        return $this->fetch();
    }

}
